package de.nordakademie.hausarbeit.crudframework.validation;

import org.eclipse.xtext.validation.Check;
import de.nordakademie.hausarbeit.crudframework.domainDsl.Application;
import de.nordakademie.hausarbeit.crudframework.domainDsl.DomainDslPackage;
import de.nordakademie.hausarbeit.crudframework.domainDsl.Entity;
import de.nordakademie.hausarbeit.crudframework.domainDsl.SimpleAttribute;


public class DomainDslJavaValidator extends AbstractDomainDslJavaValidator {
	/* =======================================
	 * Some validation rules for the domainDSL
	 *========================================/

	/*
	 * validate that an entity starts with a capital
	 */
	@Check
	public void checkEntityNameStartsWithCapital(Entity entity) {
		if (!Character.isUpperCase(entity.getName().charAt(0))) {
			error("Entity name should start with a capital", DomainDslPackage.Literals.TYPE__NAME);
		}
	}

	/*
	 * validate that the application name starts with a capital
	 */
	@Check
	public void checkApplicationNameStartsWithCapital(Application app) {
		if (!Character.isUpperCase(app.getName().charAt(0))) {
			error("Application name should start with a capital", DomainDslPackage.Literals.APPLICATION__NAME);
		}
	}
	
	/*
	 * validate that only letters are in the application name
	 */
	@Check
	public void checkApplicationNameOnlyIncludesCharacters(Application app) {
		boolean tmp = true;
		for(Character c : app.getName().toCharArray()) {
			if(!Character.isLetter(c)) {
				tmp = false;
				break;
			}
		}
		if(!tmp) {
			error("Application name should only include letters",  DomainDslPackage.Literals.APPLICATION__NAME);
		}
	}
	
	/*
	 * validate that only letters are in the application name
	 */
	@Check
	public void checkEntityNameOnlyIncludesCharacters(Entity entity) {
		boolean tmp = true;
		for(Character c : entity.getName().toCharArray()) {
			if(!Character.isLetter(c)) {
				tmp = false;
				break;
			}
		}
		if(!tmp) {
			error("Entity name should only include letters",  DomainDslPackage.Literals.TYPE__NAME);
		}
	}
	
	
	
	/*
	 * validate that the entity name is not empty
	 */
	@Check
	public void checkEntityNameIsNotEmtpy(Entity entity) {
		if(entity.getName().isEmpty()) {
			error("Name of entity cannot be empty!",DomainDslPackage.Literals.TYPE__NAME);
		}
	}
	
	/*
	 * validate that an entity should have at least one attribute
	 */
	@Check
	public void checkEntityHasAtLeastOneAttribute(Entity entity) {
		if(entity.getAttributes().isEmpty()) {
			error("entity must have at least one attribute!",DomainDslPackage.Literals.ENTITY__ATTRIBUTES);
		}
	}
	
	/*
	 * validate that an SimpleAttribute must have a type
	 */
	public void checkSimpleAttibuteHasAType(SimpleAttribute attribute) {
		if(attribute.getType().isEmpty()) {
			error("SimpleAttribute must have a type!", DomainDslPackage.Literals.SIMPLE_ATTRIBUTE__TYPE);
		}
	}
		
}
